package cn.vawn.exam.service.impl;

import cn.vawn.base.dto.PageDTO;
import cn.vawn.base.dto.RequestPageDTO;
import cn.vawn.exam.entity.PaperType;
import cn.vawn.exam.entity.QuestionsType;
import cn.vawn.exam.mapper.QuestionsTypeMapper;
import cn.vawn.exam.service.IQuestionTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/5/19 0019.
 */
@Service("questionTypeService")
public class QuestionTypeServiceImpl implements IQuestionTypeService {

    @Resource
    private QuestionsTypeMapper questionsTypeMapper;

    @Override
    public List<QuestionsType> getAllType() {
        return questionsTypeMapper.selectAllType();
    }

    @Override
    public PageDTO searchQuestionType(RequestPageDTO requestPageDTO, String typeName, Date startTime, Date endTime) {
        PageDTO pd = new PageDTO();

        int pageNumber = requestPageDTO.getPageNumber();
        int pageSize = requestPageDTO.getPageSize();
        String sort = requestPageDTO.getSort();
        String desc = requestPageDTO.getDesc();
        if (pageNumber > 0 && pageSize > 0) {
            PageHelper.startPage(pageNumber, pageSize);
        }
        if (!sort.isEmpty() && !desc.isEmpty()) {
            PageHelper.orderBy(sort + " " + desc);
        }

        List<PaperType> searchList = questionsTypeMapper.searchQuestionType(typeName, startTime, endTime);

        pd.setPageNumber(pageNumber);
        pd.setPageSize(pageSize);
        pd.setTotal((int) new PageInfo(searchList).getTotal());
        pd.setData(new PageInfo(searchList).getList());

        return pd;
    }

    @Override
    public int insert(QuestionsType questionsType) {
        return questionsTypeMapper.insert(questionsType);
    }

    @Override
    public int update(QuestionsType questionsType) {
        return questionsTypeMapper.update(questionsType);
    }

    @Override
    public QuestionsType selectById(Integer questionId) {
        return questionsTypeMapper.selectById(questionId);
    }

    @Override
    public void delete(QuestionsType questionsType) {
        questionsTypeMapper.delete(questionsType);
    }
}
